/*
 * Copyright (c) 2006-2023, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-06-29     Rbb666       first version
 */

#include <rtthread.h>
#include <rtdevice.h>

#include "drv_gpio.h"
#include "drv_usbd.h"

#define LED_PIN     GET_PIN(0, 0)

#define         USB_STACK_SIZE      2048
#define         USB_PRIORITY        5
#define         USB_TICKS           5

static rt_thread_t Thread_usb;
void thread_usb_entery(void *parameter);

int main(void)
{
    rt_pin_mode(LED_PIN, PIN_MODE_OUTPUT);

    Thread_usb = rt_thread_create("usb_mouse",thread_usb_entery, RT_NULL, USB_STACK_SIZE, USB_PRIORITY, USB_TICKS);
    if(Thread_usb != RT_NULL)
        rt_thread_startup(Thread_usb);

    for (;;)
    {
        rt_pin_write(LED_PIN, PIN_HIGH);
        rt_thread_mdelay(500);
        rt_pin_write(LED_PIN, PIN_LOW);
        rt_thread_mdelay(500);
    }
}


void thread_usb_entery(void *parameter)
{
    uint32_t counter = 0u;
    uint8_t mouse_data[3];
    rt_device_t usbd0 = RT_NULL;
    usbd0 = rt_device_find("hidd");
    if (usbd0)
        rt_device_open(usbd0, RT_DEVICE_FLAG_RDWR);
    else
        return;

    for(;;)
    {
        rt_thread_mdelay(10);
        counter++;
        if ((counter % 128) == 0)
        {
            mouse_data[1] = 5;
        }
        if ((counter % (2*128)) == 0)
        {
            mouse_data[1] = -5;
        }
        rt_device_write(usbd0, 0, mouse_data, sizeof(mouse_data));
    }
}
